#include "mainwindow.h"
#include <QMessageBox>
#include <QStyle>  // 添加这行

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("五子棋游戏");
    resize(600, 650);

    // 创建游戏棋盘
    gameBoard = new GameBoard(this);
    setCentralWidget(gameBoard);

    // 创建界面组件
    createMenus();
    createToolBar();
    createStatusBar();

    // 连接信号
    connect(gameBoard, &GameBoard::gameOver, this, &MainWindow::onGameOver);
}

MainWindow::~MainWindow()
{
}

void MainWindow::createMenus()
{
    // 创建菜单栏
    QMenuBar *menuBar = this->menuBar();

    // 游戏菜单
    QMenu *gameMenu = menuBar->addMenu("游戏(&G)");

    QAction *newGameAct = new QAction("新游戏(&N)", this);
    newGameAct->setShortcut(QKeySequence("Ctrl+N"));
    connect(newGameAct, &QAction::triggered, this, &MainWindow::newGame);
    gameMenu->addAction(newGameAct);

    gameMenu->addSeparator();

    QAction *exitAct = new QAction("退出(&X)", this);
    exitAct->setShortcut(QKeySequence("Ctrl+Q"));
    connect(exitAct, &QAction::triggered, this, &QMainWindow::close);
    gameMenu->addAction(exitAct);

    // 帮助菜单
    QMenu *helpMenu = menuBar->addMenu("帮助(&H)");

    QAction *aboutAct = new QAction("关于(&A)", this);
    connect(aboutAct, &QAction::triggered, this, &MainWindow::aboutGame);
    helpMenu->addAction(aboutAct);
}

void MainWindow::createToolBar()
{
    QToolBar *toolBar = addToolBar("工具栏");

    QAction *newGameAct = new QAction("新游戏", this);
    newGameAct->setIcon(style()->standardIcon(QStyle::SP_FileIcon));
    connect(newGameAct, &QAction::triggered, this, &MainWindow::newGame);
    toolBar->addAction(newGameAct);

    toolBar->addSeparator();

    turnLabel = new QLabel("轮到：黑棋");
    toolBar->addWidget(turnLabel);
}

void MainWindow::createStatusBar()
{
    statusLabel = new QLabel("欢迎来到五子棋游戏！");
    statusBar()->addWidget(statusLabel);
}

void MainWindow::newGame()
{
    gameBoard->resetGame();
    statusLabel->setText("新游戏开始！");
    turnLabel->setText("轮到：黑棋");
}

void MainWindow::aboutGame()
{
    QMessageBox::about(this, "关于五子棋",
                       "五子棋游戏 v1.0\n\n"
                       "这是一个用Qt Creator开发的五子棋游戏。\n"
                       "支持双人对战，具有完整的胜负判断功能。\n\n"
                       "操作说明：\n"
                       "- 黑棋先手\n"
                       "- 点击棋盘交叉点放置棋子\n"
                       "- 连成五子获胜");
}

void MainWindow::onGameOver(int winner)
{
    if (winner == BLACK) {
        statusLabel->setText("黑棋获胜！点击'新游戏'重新开始");
        turnLabel->setText("获胜：黑棋");
    } else if (winner == WHITE) {
        statusLabel->setText("白棋获胜！点击'新游戏'重新开始");
        turnLabel->setText("获胜：白棋");
    } else {
        statusLabel->setText("平局！点击'新游戏'重新开始");
        turnLabel->setText("平局");
    }
}
